package collection;

import java.util.ArrayList;
import java.util.List;
/**
 * List获取子集操作:
 *  List<T> subList(int start,int end):含头不含尾
 */
public class ListDemo3 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++){
            list.add(i*10);
        }
        System.out.println("list:"+list);  // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

        List<Integer> sublist = list.subList(3,8); // 从下标3到8，包含3，不包含8
        System.out.println("subList:"+sublist);      // subList[30, 40, 50, 60, 70]
        System.out.println("-----------------------------------");
        //将子集每个元素扩大十倍
        for (int i = 0; i < sublist.size(); i++){
            sublist.set(i,sublist.get(i)*10);
        }
        System.out.println("subList:"+sublist); // subList:[300, 400, 500, 600, 700]
        //注意：对子集的操作就是对原集合对应元素的操作
        System.out.println("list:"+list);       // list:[0, 10, 20, 300, 400, 50, 600, 70, 80, 900]
        System.out.println("-----------------------------------");
        list.set(3,1000);
        System.out.println("list:"+list);       // list:[0, 10, 20, 1000, 400, 50, 600, 70, 80, 900]
        //原集合数据改变后，子集数据跟着变了
        System.out.println("subList:"+sublist); // subList:[1000, 400, 500, 600, 700]
        System.out.println("-----------------------------------");
        list.remove(0);
        System.out.println("list:"+list);       // list:[10, 20, 1000, 400, 50, 600, 70, 80, 900]
        //原集合长度改变后，子集将不能再进行任何操作了，若操作则发生异常，但是可以重新获取子集
        //System.out.println("subList:"+sublist);
    }
}
